package daoImpl;

import baseDao.BaseDao;
import entity.Post;
import entity.Topic;

import java.util.List;

/**
 * Created by miaoyu on 2017/7/12.
 */
public class TopicDaoImpl extends BaseDao implements dao.TopicDao {
    /**
     * 加入板块
     * @param topic
     * @return 修改行数
     * 成功
     */
    public int addTopic(Topic topic) {
        Object objs[] = {topic.getTopicName()};
        return executeUpdate("insert into topic(topicName) values(?)",objs);
    }

    /**
     * 根据板块名删除板块
     * @param topicName
     * @return 修改行数
     * 成功
     */
    public int deleteTopic(String topicName) {
        Object objs[] = {topicName};
        return executeUpdate("delete from topic where topicName =?",objs);
    }

    /**
     * 查询所有板块
     * @return板块类型list
     * 成功
     */
    public List queryAllTopic() {
        List<Topic> list=executeQuery("select * from topic", null, Topic.class);
        return list;
    }

    /**
     * 根据板块名查询板块
     * @param topicName
     * @return 板块
     * 成功
     */
    public Topic queryTopicByName(String topicName) {
        List<Topic> list=executeQuery("select * from topic where topicName='"+topicName+"'", null, Topic.class);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    /**
     * 根据板块名查询板块中的所有帖子
     * @param topicID 板块ID
     * @return帖子类型list
     * 成功
     */
    public List queryAllPostsByID(int topicID) {
        List<Post> list = executeQuery("SELECT * FROM post WHERE topicID="+topicID,null,Post.class);
        return list;
    }
}
